package com.web.new12306.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.web.new12306.pojo.Ticket;
import com.web.new12306.service.TicketService;
import com.web.new12306.service.impl.TicketServiceImpl;

/**
 * @author 赵靖懿
 * Servlet implementation class adminServlet
 */
@WebServlet("/adminServlet")
public class adminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public adminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String param=request.getParameter("param");
	    if("update".equals(param))
		{
			doUpdate(request,response);
		}else if("add".equals(param)) {
			doInsert(request,response);
		}else if("delete".equals(param)) {
			doDelete(request, response);
		}
		
	}
	
	protected void doUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
					request.setCharacterEncoding("UTF-8");
					response.setContentType("text/html;charset=UTF-8");
					Ticket t=new Ticket();
					String Id=request.getParameter("ticketId");
					String trainNumber=request.getParameter("trainNumber");
					String startStation=request.getParameter("startStation");
					String terminalStation=request.getParameter("terminalStation");
					String time=request.getParameter("time");
					String money=request.getParameter("price");
					String rTickets=request.getParameter("remainingTickets");
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					PrintWriter out=response.getWriter();
					TicketService ts=null;
					try {
						ts=new TicketServiceImpl();
						Date times =sdf.parse(time);
						try {
							int ticketId=Integer.parseInt(Id);
							int price=Integer.parseInt(money);
							int remainingTickets=Integer.parseInt(rTickets);
							t.setTicketId(ticketId);
							t.setTrainNumber(trainNumber);
							t.setStartStation(startStation);
							t.setTerminalStation(terminalStation);
							t.setTime(new Timestamp(times.getTime()));
							t.setPrice(price);
							t.setRemainingTickets(remainingTickets);
							if(ts.updateTicket(t)) {
							JSONObject result=new JSONObject();
							result.put("success", "true");
							String msg=JSON.toJSONString(result);
							out.print(msg);}
							else {
								JSONObject result=new JSONObject();
								result.put("msg", "修改失败");
								String msg=JSON.toJSONString(result);
								out.print(msg);
							}
						
						}catch(NumberFormatException e)
						{
							out.print("<script>alert('修改失败')</script>");
						}
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
				}
	
	
	protected void doInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		Ticket t=new Ticket();
		String trainNumber=request.getParameter("trainNumber");
		String startStation=request.getParameter("startStation");
		String terminalStation=request.getParameter("terminalStation");
		String time=request.getParameter("time");
		String price=request.getParameter("price");
		String remainingTickets=request.getParameter("remainingTickets");
	
	   
		System.out.println(time);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		PrintWriter out=response.getWriter();
		TicketService ts=null;
		try {
			ts=new TicketServiceImpl();
			Date times = sdf.parse(time);
			try {
				int prices=Integer.parseInt(price);
				int  remainingTicket=Integer.parseInt(request.getParameter("remainingTickets"));
				
				t.setTrainNumber(trainNumber);
				t.setStartStation(startStation);
				t.setTerminalStation(terminalStation);
				t.setTime(new Timestamp(times.getTime()));
				t.setPrice(prices);
				t.setRemainingTickets(remainingTicket);
				ts.insertTicket(t);
				out.print("<script>alert('添加成功')</script>");
			}catch(NumberFormatException e)
			{
				out.print("<script>alert('添加失败')</script>");
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	

protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	request.setCharacterEncoding("UTF-8");
	response.setContentType("textml;charset=UTF-8");
	Ticket t=new Ticket();
	String ticketid=request.getParameter("id");
	System.out.println(ticketid);
	JSONObject result=new JSONObject();
	int id=Integer.parseInt(ticketid);
	PrintWriter out=response.getWriter();
	t.setTicketId(id);
	TicketService ts=new TicketServiceImpl();
	boolean flag=ts.deleteTicket(t);
	System.out.println(flag);
	if(flag) {	
	result.put("success", "true");
	String msg=JSON.toJSONString(result);
	out.print(msg);
	
	}
	
}

}
